<?php

/*
define('INIT_NO_USERS', true); // 不初始化shop中的init.session
require(EC_PATH . '/includes/init.php');
GZ_Api::authSession();
include_once(EC_PATH . '/includes/lib_transaction.php');
include_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/shopping_flow.php');
*/

/*
require(EC_PATH . '/includes/init.php');
include_once(EC_PATH . '/includes/lib_transaction.php');
*/

class AoYunSession extends SessionHandlerInterface
{
	private $handler = null;
	private $table = '';
	public function close ()
	{
		return true;
	}

	public function destroy ( $session_id )
	{
		return $this->handler->query("DELETE FROM {$this->table} WHERE sesskey='{$session_id}'");
	}

	public function gc ( int $maxlifetime )
	{
		return true;
	}

	public function open ( string $save_path , string $session_name )
	{
		global $db,$ecs;
		$this->handler = $db;
		$this->table = $ecs->table('session_data');
		return true;
	}

	public function read ( string $session_id )
	{
		return $this->handler->getOne("SELECT data FROM {$this->table} WHERE sesskey='{$session_id}'");
	}

	public function write ( string $session_id , string $session_data )
	{
		return $this->handler->autoExecute($this->table, ['data' => $session_data], 'UPDATE', "sesskey='{$session_id}'");
	}
}
session_set_save_handler(new AoYunSession(), true);
$session_id ? session_id($session_id) : define('WELCOME', true) ;
session_start();
define('INIT_NO_USERS', true);
require(EC_PATH . '/includes/init.php');
get_access() && GZ_Api::authSession();





$route = [
	'address.create' => 'address/add',
	'address.delete' => 'address/delete',
	'address.update' => 'address/update',
	'address.list'	 => 'address/list',
	'address.fetch'	 => 'address/info',
];


$config = [
	'address.create' => [
		'path' => 'address/add',
		'require' => [
			EC_PATH . '/includes/lib_transaction.php',
			ROOT_PATH . 'languages/' .$_CFG['lang']. '/shopping_flow.php'
		],
		'access' => true,
		'params' => [
			'address_id' => [
				'default' => 0,
				'type' => 'intger',
				'filter' => 'abs,'
			],
			'defalut' => [
				'type' => 'intger',
				'enum' => [0,1]
			]
		]
	];
];

function prep()
{
	$url = _GET('url');
	$conf = $config[implode('.', explode('/', $url))];
	$conf['access'] && !session('access') && abort('请先登录!');

	$params = [];
	$params_error = '';
	foreach ($conf['params'] as $name => $set)
	{
		if (isset($_POST[$name]))
		{
			$params[$name] = trim($_POST[$name]);
			switch ($set['type'])
			{
				case 'integer':
					$params[$name] = intval($params[$name]);
					break;
				case 'float':
					$params[$name] = float($params[$name]);
					break;
				case 'bool':
					$params[$name] = boolval($params[$name]);
					break;
				case 'double':
					$params[$name] = doubleval($params[$name]);
					break;
				case 'string':
				default:
					$params[$name] = strval($params[$name]);
					break;
			}
			foreach (explode(',', $set['filter']) as $filter)
			{
				$params[$name] = $filter($params[$name]);
			}
			isset($set['enum']) && !in_array($params[$name], $set['enum']) && $params_error .= "参数{$name}不合法!\n";
		}
		else
		{
			isset($set['default']) && $params[$name] = $set['default'];
			$params_error .= "参数{$name}不能为空!\n";
		}
	}
	$params_error && abort($params_error);

	foreach ($conf['require'] as $file) require $file;
	require $conf['path'];
}

function session($name, $value = '')
{
	if ($value === '')
	{
		return isset($_SESSION[$name]) ?: false;
	}
	elseif($value === null)
	{
		isset($_SESSION[$name]) && unset($_SESSION[$name]);
	}
	else
	{
		$_SESSION[$name] = $value;
	}
}

function abort($msg)
{
	exit(json_encode(['rs' => 0, 'msg' => $msg]));
}






// 登录信息
if ()
{
	# code...
}
// 包含文件

function get_access()
{

	return false;

	return true;
}

header('Content-type:appliction/json; charset=UTF-8');
exit(json_encode(['con' => $controller, 'tmp' => $tmp, 'url' => $url]));